<?php

namespace app\store\model\server;

use app\api\model\WxMessage;
use app\common\model\server\Apply as ApplyModel;
use app\common\model\UploadFile as UploadFileModel;
use app\common\service\Message as MessageService;
use app\common\enum\server\ApplyStatus as ApplyStatusEnum;
use think\Db;

/**
 * 服务人员入驻申请模型
 * Class Apply
 * @package app\store\model\server
 */
class Apply extends ApplyModel
{
    /**
     * 获取服务人员申请列表
     * @param string $search
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($search = '')
    {
        $types = Db("appointment_type")->where([
            'is_delete' => 0,
            'wxapp_id' => self::$wxapp_id
        ])->column('name', 'type_id');
        // 构建查询规则
        $this->alias('apply')
            ->field('apply.*, user.nickName, user.avatarUrl')
            ->with(['referee'])
            ->join('user', 'user.user_id = apply.user_id')
            ->order(['apply.create_time' => 'desc']);
        // 查询条件
        !empty($search) && $this->where('user.nickName|apply.real_name|apply.mobile', 'like', "%$search%");
        // 获取列表数据
        return $this->paginate(15, false, [
            'query' => \request()->request()
        ])->each(function ($item, $key) use ($types) {
            $server_name = '';
            foreach (explode(',', $item['type_ids']) as $i) {
                $server_name .= isset($types[$i]) ? $types[$i] . ',' : '';
            }
//            $images = [];
//            if($item['image_ids']){
//                $image_ids =explode(',', $item['image_ids']);
//                foreach ($image_ids as $ii){
//                    $uploadFile = new UploadFileModel;
//                    $image = $uploadFile->detail($ii);
//                    $images[] = $image['file_path'];
//                }
//
//
//            }




            $item['images'] = explode(',',$item['image_ids']);
            $server_name = rtrim($server_name, ',');
            $item['server_name'] = $server_name;
        });
    }

    /**
     * 服务人员入驻审核
     * @param $data
     * @return bool
     */
    public function submit($data)
    {
        if ($data['apply_status'] == ApplyStatusEnum::AUDIT_REJECT && empty($data['reject_reason'])) {
            $this->error = '请填写驳回原因';
            return false;
        }

        $this->transaction(function () use ($data) {
            $message = [
                'openid' => Db::name('user')->where('user_id',$this['user_id'])->value('open_id'),
                'remark' => $data['reject_reason']
            ];
            if ($data['apply_status'] == ApplyStatusEnum::AUDIT_PASS) {
                // 新增服务人员
                User::add($this['user_id'], [
                    'real_name' => $this['real_name'],
                    'mobile' => $this['mobile'],
                    'type_ids' => $this['type_ids'],
                ]);
                //发送审核通过提醒
                //推送预约信息
                WxMessage::sendMessage(8, $message);
            }else{
                //发送审核不通过提醒
                //推送预约信息
                WxMessage::sendMessage(9, $message);
            }

            // 更新申请记录
            $data['audit_time'] = time();
            $this->allowField(true)->save($data);
            // 发送订阅消息
            MessageService::send('server.apply', [
                'apply' => $this,               // 申请记录
                'user' => $this['user'],        // 用户信息
            ]);
        });
        return true;
    }

}